package com.qishuihe.common.utils.threads;

import com.qishuihe.common.utils.ListUtils;
import com.qishuihe.running.runningrespository.tablecarddata.entity.TableCardDataEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class MegerDataRunnable  implements Runnable {

    private MegerDataVO<List<TableCardDataEntity>> megerDataVO;

    private MegerDataVO<List<Map<String,String>>> returenMegerDataVO;

    public MegerDataRunnable(MegerDataVO<List<TableCardDataEntity>> megerDataVO) {
        this.megerDataVO=megerDataVO;
    }

    public void run() {
         returenMegerDataVO = new MegerDataVO<List<Map<String,String>>>();
         System.out.println("MegerDataRunnable process："+megerDataVO.getOrder());
        if(ListUtils.isEmpty(megerDataVO.getObj())){
            return ;
        }
        returenMegerDataVO.setOrder(megerDataVO.getOrder());
        List<TableCardDataEntity> ls =megerDataVO.getObj();
        Map<String,List<TableCardDataEntity>> map = ls.stream().collect(Collectors.groupingBy(TableCardDataEntity::getUuid));
        List<Map<String,String>> tempList= new ArrayList<Map<String,String>>();
        List<String> uuids = new ArrayList<String>();
        for(TableCardDataEntity tableCardDataEntity:ls) {
            if(uuids.contains(tableCardDataEntity.getUuid())) {
                tempList.get(uuids.indexOf(tableCardDataEntity.getUuid())).put(tableCardDataEntity.getCode(),tableCardDataEntity.getValue());
            }else {
                uuids.add(tableCardDataEntity.getUuid());
                Map<String,String> map_= new HashMap<String,String>();
                map_.put(tableCardDataEntity.getCode(),tableCardDataEntity.getValue());
                tempList.add(map_);
            }

        }
        returenMegerDataVO.setObj(tempList);
        sendMegerDataVO(returenMegerDataVO);

    }
   public MegerDataVO<List<Map<String,String>>> getMegerDataVO(){
        return returenMegerDataVO;
    }

    public void sendMegerDataVO(MegerDataVO<List<Map<String,String>>> returenMegerDataVO){
        //todo 业务数据发送出去与主线程通信
        System.out.println("sendMegerDataVO");
    }


}
